<!DOCTYPE html>
<html>
<head>
    <title>crypto</title>
    <meta charset="UTF-8">
    <script type="text/javascript" src="bower_components/crypto-js/crypto-js.js"></script>

</head>
<body>

<script type="text/javascript">

    var plaintText = '这是我要加密的内容'; // 明文
    var keyStr = 'zhiyu-encrypt'; // 一般key为一个字符串

    //不可逆的
    var md5=CryptoJS.MD5("Message")
    console.log("MD5",md5,md5.toString());



    /*var encrypted = CryptoJS.DES.encrypt("王晓波", "911");
     console.log(encrypted)
     var decrypted = CryptoJS.DES.decrypt(encrypted, "911");
     console.log(decrypted)*/


    var encrypted = CryptoJS.TripleDES.encrypt("Message", "Secret Passphrase");
    console.log(typeof encrypted, encrypted)
    console.log(encrypted.toString(CryptoJS.enc.Utf8)); //
    var decrypted = CryptoJS.TripleDES.decrypt(encrypted, "Secret Passphrase");
    console.log('解密结果：', typeof decrypted, decrypted)
    console.log(decrypted.toString(CryptoJS.enc.Utf8)); //



    plaintText="undefined";
    //可逆的
    var aes = CryptoJS.AES.encrypt(plaintText, keyStr);
    console.log("AES",aes,aes.toString());
    var aesr = CryptoJS.AES.decrypt(aes.toString(), keyStr).toString(CryptoJS.enc.Utf8);
    console.log("解密结果：", typeof aesr,aesr);

    var tdes = CryptoJS.TripleDES.encrypt(plaintText, keyStr);
    console.log("TripleDES",tdes,tdes.toString());
    var tdesr = CryptoJS.TripleDES.decrypt("U2FsdGVkX1+6BfPrTjQuDBP8zqLCitOeQZYwgTiBOtXsVRRnky1BQ5VEJ9ShQAf5saG+hgMr4yyMu+QTA19+kyejY9dMsnlGRTAHZUlcA87JHCmHR8jLq9r03Iea6ItXjsw39pH7Vp8jA8GA2uduX5gSFJvObJe6XAHBBBl3L1k7V4EYbwrOzPHBHnRJzMxYHiIHPGeG900ptAxbEtHly7JKAuWZsoQ6zcMFpYaMd+0VmkZNhnf4x4np1+ScqBTMBRHtVP/C0qTEdr8v6GCvlxYb6ljN1ejL30jJfqhTcpIh+M+rBWBrhI6hQFMlPot2MD7rnFOgXt8SYMu9ifYCqxsBqf8QJevIeKbpfwYFUnMoTnkWHcdorVCmh1Y7prB17kUypJPWhvearGIsrr5coJD09UkoD8zx6P6uIrgj323JNebNdWChNbJJpsZOYuqKejrqlyBX4vYs5yKJAE8+zk8pM4akFk65pFqeoIECLtiXYGBnaSlklPVWBSGDNsA7kWx3KYbebST438bg7u5KAw/CoH8Mz89djT5LF1fquBz+ookMiUUYSJcsdUmp7cbvdPrAwjUDfHbbMfxefcoW1gudSDdN7H1klesSIBfn20j6ZCF6aHVeZLiztr5YkisWHZoRcGDsUpuR/SZFzi5kvGT+roIYS3YLRs+jZ2CpobAfe3aq/BrJlxgqWlkgAiDzA2/EyrOBeObWnxBsa8tPcQ7wbN3C4pCtk3knWUnY+y2Ham2hcw2wBHusG27Xb1CuXHDepF0EAO4LbTQtRRdqIrQ8xae6PYZheSlBEXcUTylfzbxKF3z/3Q==", keyStr).toString(CryptoJS.enc.Utf8);
    console.log("解密结果：", typeof tdesr, tdesr);

if(plaintText && typeof plaintText=="string")
    console.log("is string");


console.log('===============================')

//encrypt
var rawStr = "123456789";
var wordArray = CryptoJS.enc.Utf8.parse(rawStr);
var base64 = CryptoJS.enc.Base64.stringify(wordArray);
console.log('encrypted:',typeof base64, base64);

//decrypt
var parsedWordArray = CryptoJS.enc.Base64.parse(base64);
var parsedStr = parsedWordArray.toString(CryptoJS.enc.Utf8);
console.log("parsed:",parsedStr);
</script>
</body>
</html>